package classes.controlers {
	import classes.managers.Game_Manager;
	import classes.sounds.SoundCollection;
	import flash.display.MovieClip;
	import flash.events.Event;
	/**
	 * ...
	 * @author Kihoonee
	 */
	public class Rope_Controler extends A_Controler {
		
		private var up:MovieClip;
		private var down:MovieClip;
		
		private var _UP_ROPE_SPEED:String = "속도5";
		
		private var cnt:int = 0;
		private var randomSpeedCnt:int = 0;
		private var _time:Number = 0;
		
		public function Rope_Controler( _gm:Game_Manager, _mc:MovieClip ) {
			super( _gm, _mc );
			
			up = mc.upRope_mc;
			down = mc.downRope_mc;
			
			up.gotoAndStop(1);
			down.gotoAndStop(1);
			
			addEvent();
		}
		
		public function init():void {
			cnt = 0;
			randomSpeedCnt = 0;
			time = 1.2;
			UP_ROPE_SPEED = "속도5";
			
			up.gotoAndStop(1);
			down.gotoAndStop(1);
			
			up.visible = true;
			down.visible = false;
		}
		
		public function twist():void {
			
			up.gotoAndPlay( UP_ROPE_SPEED );
		}
		
		public function stop():void {
			up.stop();
			down.stop();
		}
		
		public function ropeSpeed( _rabbitCnt:int ):void {
			switch ( _rabbitCnt ) {
				case 1: UP_ROPE_SPEED = "속도5"; time = 1.2;
				break;
				case 2: UP_ROPE_SPEED = "속도5"; time = 1.2;
				break;
				case 3: UP_ROPE_SPEED = "속도5"; time = 1.2;
				break;
				case 4: UP_ROPE_SPEED = "속도5"; time = 1.2;
				break;
				case 5: UP_ROPE_SPEED = "속도5"; time = 1.2;
				break;
				case 6: UP_ROPE_SPEED = "속도5"; time = 1.2;
				break;
				case 7: UP_ROPE_SPEED = "속도1"; time = 2;
					ropeRandomSpeed();
				break;
				default:
			}
		}
		
		private function ropeRandomSpeed():void {
			if ( randomSpeedCnt < 3 ) {
				UP_ROPE_SPEED = "속도1";  time = 2;
			}else if ( randomSpeedCnt >= 3 && randomSpeedCnt < 6 ) {
				UP_ROPE_SPEED = "속도2";  time = 1.5;
			}else if ( randomSpeedCnt >= 6 && randomSpeedCnt < 9 ) {
				UP_ROPE_SPEED = "속도3";  time = 1.4;
			}else if ( randomSpeedCnt >= 9 && randomSpeedCnt < 14 ) {
				UP_ROPE_SPEED = "속도4";  time = 1.3;
			}else if ( randomSpeedCnt >= 14 && randomSpeedCnt < 18 ) {
				UP_ROPE_SPEED = "속도5";  time = 1.2;
			}else if ( randomSpeedCnt >= 18 && randomSpeedCnt < 23 ) {
				UP_ROPE_SPEED = "속도6";  time = 1.1;
			}else if ( randomSpeedCnt >= 23 && randomSpeedCnt < 28 ) {
				UP_ROPE_SPEED = "속도7";  time = 1;
			}else if ( randomSpeedCnt >= 28 && randomSpeedCnt < 32  ) {
				UP_ROPE_SPEED = "속도8";  time = 0.9;
			}else if ( randomSpeedCnt >= 32 && randomSpeedCnt < 36  ) {
				UP_ROPE_SPEED = "속도9";  time = 0.8;
			}else if ( randomSpeedCnt >= 36 ) {
				UP_ROPE_SPEED = "속도10";  time = 0.75;
			}
			randomSpeedCnt++;
		}
		
		private function RopeEvent(e:Event):void {
			if ( e.type == "upRope_START" ) { // 스페이스바 누를수 있는 타이밍시작.
				if ( cnt > 0 ) {
					gm.upRope_START();
					
				}
				gm.swapRope1();
				
				cnt++;
			}else if ( e.type == "upRope_END" ) {  // 스페이스바 누를수 있는 타이밍끝.
				up.visible = false;
				up.gotoAndStop( UP_ROPE_SPEED ); 
				down.visible = true;
				down.gotoAndPlay(1);
				SoundCollection.ropeSound();
				
				if ( cnt > 1 ) gm.upRope_END();
				
				
				
			}else if ( e.type == "downRope_START" ) {  // 토끼가 줄 넘는 모션 시작.
				if ( cnt > 1 ) {
					gm.downRope_START();
					
				}
				gm.swapRope2();
				
			}
			else if ( e.type == "downRope_END" ) {  // 모션 끝.
				up.visible = true;
				up.gotoAndPlay( UP_ROPE_SPEED );
				
				down.visible = false;
				down.gotoAndStop(1);
				
				SoundCollection.spaceStartSound();
				
				if ( cnt > 1 ) gm.downRope_END();
			}
		}
		
		private function addEvent():void {
			up.addEventListener( "upRope_START" , RopeEvent );
			up.addEventListener( "upRope_END" , RopeEvent );
			
			down.addEventListener( "downRope_START", RopeEvent );
			down.addEventListener( "downRope_END", RopeEvent );
		}
		
		public function get UP_ROPE_SPEED():String {
			return _UP_ROPE_SPEED;
		}
		
		public function set UP_ROPE_SPEED(value:String):void {
			_UP_ROPE_SPEED = value;
		}
		
		public function get time():Number {
			return _time;
		}
		
		public function set time(value:Number):void {
			_time = value;
		}
	}
}